﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


namespace PersianCalendarControl
{
    public enum CalendarControlType
    {
        HeaderLabel = 1,
        HeaderNextButton = 2,
        HeaderPrevButton = 3,
        DayLabel = 4,
        MonthLabel = 5,
        YearLabel = 6,
        DecadeLabel = 7
    }

	public partial class CalendarItemLabelButton : UserControl
	{
        public delegate void CalendarItemClickedEvent(object sender, RoutedEventArgs e);

        public event CalendarItemClickedEvent OnCalendarItemClicked;

	    

        private PersianCalendar calendar;
	    private int _cellIndex;

	    private DateTime currentDate = DateTime.Now ;
        public DateTime CurrentDate
        {
            get { return currentDate; }
            set
            {
                currentDate = value;
                Refresh();
            }
        }

	    private CalendarControlType _currentCalendarControlType ;
        public CalendarControlType CurrentCalendarControlType
        {
            get { return _currentCalendarControlType; }
            set { _currentCalendarControlType = value; }
        }

	    
	    public string Text
	    {
            get { return (string)btnContent.Content; }
            set { btnContent.Content = value; }
	    }

	    public int CellIndex
	    {
	        get { return _cellIndex; }
	        set { _cellIndex = value; }
	    }

	    


	    public CalendarItemLabelButton()
		{
			// Required to initialize variables
			InitializeComponent();
            calendar = new PersianCalendar();
		}

        private void btnContent_Click(object sender, RoutedEventArgs e)
        {
            if (OnCalendarItemClicked != null) OnCalendarItemClicked(this, e);
        }

	   
        private void Refresh()
        {
            switch (_currentCalendarControlType)
            {
                case CalendarControlType.DayLabel :
                    FillDay();
                    break;
                case CalendarControlType.MonthLabel:
                    FillMonth();
                    break;
                case CalendarControlType.YearLabel:
                    FillYear();
                    break;
                case CalendarControlType.DecadeLabel:
                    FillDecade();
                    break;
            }

        }
         


        private void FillDay()
        {

            PersianCalendar persianCalendar = new PersianCalendar(); 

            int newDay = persianCalendar.GetDayOfMonth(CurrentDate);
            int newMonth = persianCalendar.GetMonth(CurrentDate);
            int newYear = persianCalendar.GetYear(CurrentDate);



          


            
            //DateTime nextMonthDate = persianCalendar.ToDateTime(newYear, newMonth + 1, newDay, 0, 0, 0, 0);
            DateTime nextMonthDate = persianCalendar.AddMonths(CurrentDate, 1);
            //DateTime prevMonthDate = persianCalendar.ToDateTime(newYear, newMonth - 1, newDay, 0, 0, 0, 0);
            DateTime prevMonthDate = persianCalendar.AddMonths(CurrentDate, -1);
            DateTime currentMonthDate = persianCalendar.ToDateTime(newYear, newMonth, 1, 0, 0, 0, 0);

            int startIndex = (int)currentMonthDate.DayOfWeek;
            if (startIndex == 6) startIndex = -1;
            int dayNum = _cellIndex - startIndex - 7;

            this.Opacity = 1;
            int pmd = calendar.GetMonth(prevMonthDate);

            if (calendar.GetMonth(prevMonthDate) < 12 && dayNum <= 0)
            {
                dayNum = dayNum + calendar.GetDaysInMonth(calendar.GetYear(prevMonthDate), calendar.GetMonth(prevMonthDate) + 1);
                currentDate = calendar.AddDays(prevMonthDate, dayNum - calendar.GetDayOfMonth(prevMonthDate));
                this.Opacity = 0.5;
            
            }
            else if (calendar.GetMonth(currentMonthDate) < 12 && dayNum > calendar.GetDaysInMonth(calendar.GetYear(currentMonthDate), calendar.GetMonth(currentMonthDate) + 1))
            {
                dayNum = dayNum - calendar.GetDaysInMonth(calendar.GetYear(currentMonthDate), calendar.GetMonth(currentMonthDate) + 1);
                currentDate = calendar.AddDays(nextMonthDate, dayNum - calendar.GetDayOfMonth(currentMonthDate));
                this.Opacity = 0.5;
            }
            if (calendar.GetMonth(prevMonthDate) >= 12 && dayNum <= 0)
            {
                dayNum = dayNum + calendar.GetDaysInMonth(calendar.GetYear(prevMonthDate), 12);
                currentDate = calendar.AddDays(prevMonthDate, dayNum - calendar.GetDayOfMonth(prevMonthDate));
                this.Opacity = 0.5;

            }
            else
            {
                currentDate = calendar.AddDays(currentMonthDate, dayNum - calendar.GetDayOfMonth(currentMonthDate));
            }
            
            this.rectCurrentSelection.Visibility = System.Windows.Visibility.Collapsed;
            if (currentDate.Date == DateTime.Now.Date)
            {
                this.rectCurrentSelection.Visibility = System.Windows.Visibility.Visible;
            
            }
            
//            if (currentDate.Date == MainCurrentDate.Date)
//            {
//                this.Focus(); 
//            }

            this.FontSize = 16;
            this.Text = dayNum.ToString();

//
//
//            DateTime MainCurrentDate = CurrentDate;
//            DateTime pdc = CurrentDate;
//            int currentDay = calendar.GetDayOfMonth(CurrentDate);
//            
//           
//            DateTime pdp = DateTime.MinValue;
//            DateTime pdn = DateTime.MinValue;
//            pdc = calendar.AddDays(pdc, (1 - calendar.GetDayOfMonth(pdc)));
//
//            pdp = calendar.AddMonths(pdc, -1);
//            pdn = calendar.AddMonths(pdc, 1);
//
//
//            int startIndex = (int)pdc.DayOfWeek;
//            if (startIndex == 6) startIndex = -1;
//
//            int dNum = _cellIndex - startIndex - 7;
//
//            this.Opacity = 1;
//            if (calendar.GetMonth(pdp) < 12 && dNum <= 0)
//            {
//                dNum = dNum + calendar.GetDaysInMonth(calendar.GetYear(pdp), calendar.GetMonth(pdp)+1);
//                currentDate = calendar.AddDays(pdp, dNum - calendar.GetDayOfMonth(pdp));
//                this.Opacity = 0.5;
//
//            }
//            else if (calendar.GetMonth(pdc) < 12 && dNum > calendar.GetDaysInMonth(calendar.GetYear(pdc), calendar.GetMonth(pdc) + 1))
//            {
//                dNum = dNum - calendar.GetDaysInMonth(calendar.GetYear(pdc), calendar.GetMonth(pdc)+1);
//                currentDate = calendar.AddDays(pdn, dNum - calendar.GetDayOfMonth(pdc));
                //cStr = calendar.GetYear(dayDateTime) + "-" + calendar.GetMonth(dayDateTime) + "-" + calendar.GetDayOfMonth(dayDateTime);
//                this.Opacity = 0.5;
//            }
//            else
//            {
//                currentDate = calendar.AddDays(pdc, dNum - calendar.GetDayOfMonth(pdc));
//            }
//
//            this.rectCurrentSelection.Visibility = System.Windows.Visibility.Collapsed;
//            if (currentDate.Date == DateTime.Now.Date)
//            {
//                this.rectCurrentSelection.Visibility = System.Windows.Visibility.Visible;
//
//            }
//
//            if (currentDate.Date == MainCurrentDate.Date)
//            {
//                this.Focus(); 
//            }

            
        }

        private void FillMonth()
        {
            DateTime MainCurrentDate = CurrentDate;
            int currentMonth = calendar.GetMonth(CurrentDate);
            this.currentDate = calendar.ToDateTime(calendar.GetYear(CurrentDate), CellIndex + 1, 1, 0, 0, 0, 0);
            this.FontSize = 14;
            this.Text = calendar.MonthNames[CellIndex+1];
            this.rectCurrentSelection.Visibility = System.Windows.Visibility.Collapsed;
            
            if (calendar.GetMonth(DateTime.Now) == calendar.GetMonth(currentDate))
            {
                this.rectCurrentSelection.Visibility = System.Windows.Visibility.Visible;   
            }
           
            if (calendar.GetMonth(MainCurrentDate) == calendar.GetMonth(currentDate))
            {
                this.Focus();
            }
        }

	    private void FillYear()
	    {
            DateTime MainCurrentDate = CurrentDate;
            this.FontSize = 14;
	        int currentYear = calendar.GetYear(CurrentDate);
	        currentDate = calendar.ToDateTime((currentYear + _cellIndex - 6), 1, 1, 0, 0, 0, 0);
	        this.Text = (currentYear + _cellIndex - 6).ToString();

            if (calendar.GetYear(DateTime.Now) == calendar.GetYear(currentDate))
            {
                this.rectCurrentSelection.Visibility = System.Windows.Visibility.Visible;
            }

            this.rectCurrentSelection.Visibility = System.Windows.Visibility.Collapsed;


            if (calendar.GetYear(MainCurrentDate) == calendar.GetYear(currentDate))
            {
                this.Focus();

            }
	    }

        private void FillDecade()
        {
            this.FontSize = 14;
            int currentYear = calendar.GetYear(CurrentDate);

            this.Text = ((currentYear + (_cellIndex * 12)) - 6) + "-" + ((currentYear)+(_cellIndex * 12) + 5);

            this.Text = (currentYear + _cellIndex - 6).ToString();
        }
	}
}